//
//  HMVistorView.swift
//  01-启动图片的搭建
//
//  Created by 白利兵 on 15/10/14.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class HMVistorView: UIView {
    // 判断是否为首页，并分别设置标题文字和头像图片以及遮罩图
    func setupInfo(imageName: String?, title: String) {
        titleLabel.text = title
        guard let imgName = imageName else {
            // 开始动画
            ImageStartAnimating()
            return
        }
        // 替换动画图片
        animImage.image = UIImage(named: imgName)
        // 隐藏小房子
        homeImage.hidden = true
        sendSubviewToBack(maskImage)
        
    }
    private func ImageStartAnimating() {
        // 创建基础动画
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画属性
        animate.toValue = 2 * M_PI
        animate.duration = 20
        animate.repeatCount = MAXFLOAT
        // 自动销毁动画（当视图销毁时）
        animate.removedOnCompletion = false
        // 为图片添加动画
        animImage.layer.addAnimation(animate, forKey: nil)
    }
    // 重写构造方法
    override init(frame: CGRect) {
       super.init(frame: frame)
        // 设置界面
        setUI()
    }
    // 解码的过程
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // 设置界面
        setUI()
    }
    // 懒加载小房子图片框
    private lazy var homeImage: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    // 懒加载动画图片
    private lazy var animImage: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
//    UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 懒加载遮罩图片
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 懒加载titleLabel
    private lazy var titleLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGrayColor())
    // 懒加载注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册",
        fontSize: 18,
        color: UIColor.orangeColor(),
        backImageName: "common_button_white_disable", backColor: UIColor.clearColor())
    // 懒加载登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录",
        fontSize: 18,
        color: UIColor.darkGrayColor(),
        backImageName: "common_button_white_disable", backColor: UIColor.clearColor())
}
extension HMVistorView {
    private func setUI() {
        // 添加控件(先后顺序要注意，遮罩图片要放在动画上面，层级关系)
        addSubview(animImage)
        addSubview(maskImage)
        addSubview(homeImage)
        addSubview(titleLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置自动布
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置动画图片的约束
        addConstraint(NSLayoutConstraint(item: animImage,
            attribute: .CenterX,
            relatedBy: .Equal,
            toItem: self,
            attribute: .CenterX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(item: animImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // 设置小房子的约束
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: .CenterX, relatedBy: .Equal, toItem: animImage, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: .CenterY, relatedBy: .Equal, toItem: animImage, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 设置titleLabel约束
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: animImage, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: animImage, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        // 设置登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        // 设置遮罩图片的约束
        addConstraint(NSLayoutConstraint(item: maskImage, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImage, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImage, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImage, attribute: .Bottom, relatedBy: .Equal, toItem: animImage, attribute: .Bottom, multiplier: 1.0, constant: -10))
        // 设置背景色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
}

















